#include "stdafx.h"
#include "ConnectTipView.h"
#include "ColorAnimation.h"

ConnectTipView::ConnectTipView(View* parent)
	: View(parent)
	, is_connect_(false)
	, connect_color_1(0, 80, 0)
	, connect_color_2(0, 150, 0)
    , disconnect_color_1(132, 0, 0)
	, disconnect_color_2(255, 0, 0)
	, anmation_change_(false)

{
	anmation_ = std::shared_ptr<ColorAnimation>(new ColorAnimation(10, 100));
	anmation_->setDelegate(this);
}


ConnectTipView::~ConnectTipView()
{
}

void ConnectTipView::setConnect(bool connect)
{
	is_connect_ = connect;
}

bool ConnectTipView::isConnect()
{
	return is_connect_;
}

bool ConnectTipView::paint(SkCanvas* canvas)
{
	// 
	SkColor tempColor;
	if (anmation_->isAnimate())
	{
		tempColor = anmation_->getTempColor();
		canvas->FillRect(tempColor, 0, 0, Width(), Height());
	}
	else
	{
		calcColorAndStartAnimate();
	}

	SkFont font(std::wstring(L"Arial"));
	std::wstring text;
	if (isConnect())
	{
		text = L"Welcome use the software - Connectted";
	} 
	else
	{
		text = L"USB Device Disconnect";
	}
	canvas->drawText(text, font, SkColor(255, 255, 255), 0, 0, Width(), Height());

	return true;
}

void ConnectTipView::animateStarted(Animation* animation)
{

}

void ConnectTipView::animateCompleted(Animation* animation)
{
	calcColorAndStartAnimate();
}

void ConnectTipView::animateStep(Animation* animation)
{

}

void ConnectTipView::animateCanceled(Animation* animation)
{

}

void ConnectTipView::calcColorAndStartAnimate()
{
	SkColor color1;
	SkColor color2;
	if (isConnect())
	{
		color1 = connect_color_1;
		color2 = connect_color_2;
	}
	else
	{
		color1 = disconnect_color_1;
		color2 = disconnect_color_2;
	}

	if (anmation_change_)
	{
		anmation_->AnimateColorTo(this, color1, color2);
	}
	else
	{
		anmation_->AnimateColorTo(this, color2, color1);
	}

	anmation_change_ = !anmation_change_;
}
